/*******************************************************************************
 * BanneddomainsAction.java
 *
 * Created by Nishan
 * Last modified Date: Sep 3, 2008 12:13:24 PM by Ankur
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/
package com.printeverywhere.action;

import java.util.List;

import org.apache.commons.validator.EmailValidator;
import org.apache.log4j.Logger;

import com.printeverywhere.delegator.BanneddomainsDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.Banneddomain;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class BanneddomainsAction.
 */
public class BanneddomainsAction extends AdminSystemSetup  {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(BanneddomainsAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The banneddomain. */
	private Banneddomain banneddomain = new Banneddomain();

	/** The banneddomainlist. */
	private List<Banneddomain> banneddomainlist = BanneddomainsDelegator.getBannedDomain();

	/** The block domain name. */
	private String blockDomainName = null;

	/** The delete block domain name. */
	private String deleteBlockDomainName = null;

	/** The editlist. */
	private String editlist;

	/** The id. */
	private Integer id;

	/** The domainid. */
	private Integer domainid = 0;

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Show the banned domain.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String show() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("show() - start"); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("show() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Edits the banned domain selected by Admin.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String edit() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("edit() - start"); //$NON-NLS-1$
		}

		Banneddomain blockDomain = BanneddomainsDelegator.getBannedDomainbyId(id);
		blockDomainName = blockDomain.getDomainName();
		domainid = blockDomain.getId();

		if (logger.isDebugEnabled()) {
			logger.debug("edit() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Delete a banned domain.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String delete() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("delete() - start"); //$NON-NLS-1$
		}

		Banneddomain blockDomain = BanneddomainsDelegator.getBannedDomainbyId(id);
		deleteBlockDomainName = blockDomain.getDomainName();
		domainid = blockDomain.getId();
		deleteBlockedDomain();
		BanneddomainsDelegator.deleteBannedDomain(banneddomain);
		banneddomainlist = BanneddomainsDelegator.getBannedDomain();

		if (logger.isDebugEnabled()) {
			logger.debug("delete() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Adds the domain to banned domain list.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String addDomain() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("addDomain() - start"); //$NON-NLS-1$
		}

		if (blockDomainName == null || blockDomainName.length() == 0  ){
			addActionError("ERROR While Inserting Blocked Domain Name".intern());

			if (logger.isDebugEnabled()) {
				logger.debug("addDomain() - end"); //$NON-NLS-1$
			}
			return INPUT;
		}
		String e = blockDomainName;
		if(e.startsWith("*")){
			e = e.replaceFirst("/*", "a");
		}
		EmailValidator ee = EmailValidator.getInstance();
		if(!ee.isValid(e)){
			addActionError("Please enter a proper Domain Name or email address".intern());
			if (logger.isDebugEnabled()) {
				logger.debug("addDomain() - end"); //$NON-NLS-1$
			}
			return INPUT;
		}
		
		if(domainid < 1){
		SearchCriteria searchCriteria=new SearchCriteria();
		searchCriteria.setAttribute("domainName".intern(), blockDomainName);
		List list = CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, Banneddomain.class);
		if (list!=null && list.size() > 0){
			addActionError("Domain Name already exist".intern());
			if (logger.isDebugEnabled()) {
				logger.debug("addDomain() - end"); //$NON-NLS-1$
			}
			return INPUT;
		}
		}
		
		if(domainid > 0){
			updateBlockedDomain();
			BanneddomainsDelegator.updateBannedDomain(banneddomain);
			banneddomainlist = BanneddomainsDelegator.getBannedDomain();
			blockDomainName = null;

			if (logger.isDebugEnabled()) {
				logger.debug("addDomain() - end"); //$NON-NLS-1$
			}
			return SUCCESS;
		}else{
			setBlockedDomain();
			BanneddomainsDelegator.addBannedDomain(banneddomain);
			banneddomainlist = BanneddomainsDelegator.getBannedDomain();
			blockDomainName = null;

			if (logger.isDebugEnabled()) {
				logger.debug("addDomain() - end"); //$NON-NLS-1$
			}
			return SUCCESS;
		}
	}

	/**
	 * Gets the banneddomainlist.
	 *
	 * @return the banneddomainlist
	 */
	public List<Banneddomain> getBanneddomainlist() {
		return banneddomainlist;
	}

	/**
	 * Sets the banneddomainlist.
	 *
	 * @param banneddomainlist the new banneddomainlist
	 */
	public void setBanneddomainlist(List<Banneddomain> banneddomainlist) {
		this.banneddomainlist = banneddomainlist;
	}

	/**
	 * Gets the block domain name.
	 *
	 * @return the block domain name
	 */
	public String getBlockDomainName() {
		return blockDomainName;
	}

	/**
	 * Sets the block domain name.
	 *
	 * @param blockDomainName the new block domain name
	 */
	public void setBlockDomainName(String blockDomainName) {
		this.blockDomainName = blockDomainName;
	}

	/**
	 * Gets the banneddomain.
	 *
	 * @return the banneddomain
	 */
	public Banneddomain getBanneddomain() {
		return banneddomain;
	}

	/**
	 * Sets the banneddomain.
	 *
	 * @param banneddomain the new banneddomain
	 */
	public void setBanneddomain(Banneddomain banneddomain) {
		this.banneddomain = banneddomain;
	}

	/**
	 * Sets the blocked domain.
	 */
	private void setBlockedDomain(){
		if (logger.isDebugEnabled()) {
			logger.debug("setBlockedDomain() - start"); //$NON-NLS-1$
		}
		try {
			banneddomain.setDomainName(blockDomainName);
		}catch (Exception e) {
			logger.error("setBlockedDomain()", e); //$NON-NLS-1$
			addActionError("Error occured while setting Blocked Domain and Email name : " + e.getMessage());
		}
		if (logger.isDebugEnabled()) {
			logger.debug("setBlockedDomain() - end"); //$NON-NLS-1$
		}
	}

	/**
	 * Update blocked domain.
	 */
	private void updateBlockedDomain(){
		if (logger.isDebugEnabled()) {
			logger.debug("updateBlockedDomain() - start"); //$NON-NLS-1$
		}
		try {
			banneddomain.setId(domainid);
			banneddomain.setDomainName(blockDomainName);
		}catch (Exception e) {
			addActionError("Error occured while updating Blocked Domain and Email name : " + e.getMessage());
			logger.error("updateBlockedDomain()", e); //$NON-NLS-1$
			addActionError("Error occured while setting Blocked Domain and Email name : " + e.getMessage());
		}
		if (logger.isDebugEnabled()) {
			logger.debug("updateBlockedDomain() - end"); //$NON-NLS-1$
		}
	}

	/**
	 * Delete blocked domain.
	 */
	private void deleteBlockedDomain(){
		if (logger.isDebugEnabled()) {
			logger.debug("deleteBlockedDomain() - start"); //$NON-NLS-1$
		}
		try {
			banneddomain.setId(domainid);
			banneddomain.setDomainName(deleteBlockDomainName);
		}catch (Exception e) {
			addActionError("Error occured while deleting Blocked Domain and Email name : " + e.getMessage());
			logger.error("deleteBlockedDomain()", e); //$NON-NLS-1$
			addActionError("Error occured while setting Blocked Domain and Email name : " + e.getMessage());
		}
		if (logger.isDebugEnabled()) {
			logger.debug("deleteBlockedDomain() - end"); //$NON-NLS-1$
		}
	}

	/**
	 * Gets the editlist.
	 *
	 * @return the editlist
	 */
	public String getEditlist() {
		return editlist;
	}

	/**
	 * Sets the editlist.
	 *
	 * @param editlist the new editlist
	 */
	public void setEditlist(String editlist) {
		this.editlist = editlist;
	}

	/**
	 * Gets the domainid.
	 *
	 * @return the domainid
	 */
	public Integer getDomainid() {
		return domainid;
	}

	/**
	 * Sets the domainid.
	 *
	 * @param domainid the new domainid
	 */
	public void setDomainid(Integer domainid) {
		this.domainid = domainid;
	}

	/**
	 * Gets the delete block domain name.
	 *
	 * @return the delete block domain name
	 */
	public String getDeleteBlockDomainName() {
		return deleteBlockDomainName;
	}

	/**
	 * Sets the delete block domain name.
	 *
	 * @param deleteBlockDomainName the new delete block domain name
	 */
	public void setDeleteBlockDomainName(String deleteBlockDomainName) {
		this.deleteBlockDomainName = deleteBlockDomainName;
	}
}